<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" media-type="text/plain"/>
	<xsl:template match="/">
		<busMatrix>
			<xsl:for-each select="//BusinessModel/@name">
				<BusinessCatalog>
					<BusinessCatalogID>
						<xsl:value-of select="."/>
					</BusinessCatalogID>
					<xsl:variable name="BMMLID" select="../@id"/>
					<xsl:variable name="BMMLname" select="."/>
					<!-- copying all subject areas using this business model as source -->
					<PresentationCatalogIDList>
						<xsl:for-each select="../..//PresentationCatalog/RefBusinessModel[@id = $BMMLID]">
							<PresentationCatalogID>
								<xsl:value-of select="../@name"/>
							</PresentationCatalogID>
						</xsl:for-each>
					</PresentationCatalogIDList>
					<!-- counting the number of joins of logical dimension tables -->
					<LogicalTableIDList>
						<xsl:for-each select="../..//LogicalTable[@parentId = $BMMLID]">
							<LogicalTableID>
								<xsl:variable name="LgclTblID" select="@id"/>
								<xsl:attribute name="joins">
									<xsl:value-of select="count(../..//LogicalComplexJoin/Roles/Role/RefLogicalTable[@id = $LgclTblID and contains(@qualifiedName, 'Dim')])"/>
								</xsl:attribute>
								<xsl:value-of select="$BMMLname"/>.<xsl:value-of select="@name"/>
							</LogicalTableID>
						</xsl:for-each>
					</LogicalTableIDList>
				</BusinessCatalog>
			</xsl:for-each>
			<!-- copying all logical joins (simplified view) -->
			<xsl:for-each select="//LogicalComplexJoin">
				<LogicalJoin>
					<xsl:attribute name="type"></xsl:attribute>
					<xsl:for-each select=".//RefLogicalTable">
						<LogicalTableID>
							<xsl:attribute name="type">
								<xsl:choose>
									<xsl:when test="contains(@qualifiedName, 'Dim') or contains(@qualifiedName, 'DIM')">DIM</xsl:when>
									<xsl:otherwise>FACT</xsl:otherwise>
								</xsl:choose>
							</xsl:attribute>
							<xsl:variable name="logicaltable" select="@id"/>
							<xsl:variable name="bizmodel" select="../../../../LogicalTable[@id = $logicaltable]/@parentId"/>
							<xsl:value-of select="../../../../BusinessModel[@id = $bizmodel]/@name"/>.<xsl:value-of select="../../../../LogicalTable[@id = $logicaltable]/@name"/>
						</LogicalTableID>
					</xsl:for-each>
				</LogicalJoin>
			</xsl:for-each>
		</busMatrix>
	</xsl:template>
</xsl:stylesheet>
